Imports System.IO
Imports NUnit.Framework
Imports System.Xml.Serialization

<TestFixture()> _
Public Class TodoListTest
  Dim mTodoList As Model.Todolist

  <SetUp()> _
  Public Sub Initialize()
    Using lReader As TextReader = New StreamReader(Me.GetType().Assembly.GetManifestResourceStream("ToDoList.Tests.TestList.tdl"))

      Dim lSerializer As New XmlSerializer(GetType(Model.Todolist))
      mTodoList = CType(lSerializer.Deserialize(lReader), Model.Todolist)
    End Using
    Assert.IsNotNull(mTodoList, "Deserialization failed")
  End Sub

  <Test()> _
  Public Sub TestTodolistProperties()

    Assert.IsNotNull(mTodoList.Tasks, "Tasks not found")
    Assert.AreEqual(3, mTodoList.Tasks.Length, "Not all tasks were deserialized")

    'check the properties
    Assert.AreEqual("8", mTodoList.FileFormat, "Fileformat error")
    Assert.AreEqual("Test project", mTodoList.ProjectName, "Projectname error")
    Assert.AreEqual(13, mTodoList.NextUniqueId, "Nextuniqueid error")
    Assert.AreEqual(39563, mTodoList.EarliestDueDate, "earliest due date error")
    Assert.Less(0, mTodoList.FileVersion, "Fileversion error")
    Assert.AreEqual(#3/7/2008#, mTodoList.LastModified, "Lastmodified error")

  End Sub

  <Test()> _
  Public Sub TestTaskProperties()

    'the first task has everything
    Dim lTask As Model.Task = mTodoList.Tasks(0)

    With lTask
      Assert.IsNotNull(.Tasks, "Subtasks not deserialized")
      Assert.AreEqual(1, .Tasks.Length, "Subtask not found")
      Assert.IsNotNull(.Recurrence, "Recurrence not deserialized")

      Assert.AreEqual(15732480, .PriorityColor, "PRIORITYCOLOR error")
      Assert.AreEqual("8-3-2008", .StartDateString, "STARTDATESTRING error")
      Assert.AreEqual(8, .Risk, "RISK error")
      Assert.AreEqual("private", .Category1, "Category1 error")
      Assert.AreEqual("7-3-2008 (15:34:36)", .LastModifiedString, "LASTMODSTRING error")
      Assert.AreEqual(5, .Priority, "PRIORITY error")
      Assert.AreEqual("open", .Status, "STATUS error")
      Assert.AreEqual(3, .NumPerson, "NUMPERSON error")
      Assert.AreEqual(100, .CalcPercentDone, "CALCPERCENTDONE error")
      Assert.AreEqual("janwillem", .AllocatedBy, "ALLOCATEDBY error")
      Assert.AreEqual("nogiemand", .Person1, "person1 error")
      Assert.AreEqual(39514.64902778, .LastModified, "LASTMOD error")
      Assert.AreEqual("7-3-2008", .CreationDateString, "CREATIONDATESTRING error")
      Assert.AreEqual("piet", .Person2, "Person2 error")
      Assert.AreEqual("Task with all properties", .Title, "TITLE error")
      Assert.AreEqual(39514.0, .CreationDate, "CREATIONDATE error")
      Assert.AreEqual("I", .TimeSpentUnits, "TIMESPENTUNITS error")
      Assert.AreEqual("1325", .ExternalId, "EXTERNALID error")
      Assert.AreEqual("D", .TimeEstUnits, "TIMEESTUNITS error")
      Assert.AreEqual("janwillem", .Person, "PERSON error")
      Assert.AreEqual(1.0, .TimeEstimate, "TIMEESTIMATE error")
      Assert.AreEqual(0.83333333, .CalcTimeSpent, "CALCTIMESPENT error")
      Assert.AreEqual(2, .NumCategory, "NUMCATEGORY error")
      Assert.AreEqual("home", .Category, "CATEGORY error")
      Assert.AreEqual("PLAIN_TEXT", .CommentsType, "COMMENTSTYPE error")
      Assert.AreEqual("23fdsf", .Version, "Version error")
      Assert.AreEqual("fdsafd", .Comments, "COMMENTS error")
      Assert.AreEqual("#000FF0", .PriorityWebColor, "PRIORITYWEBCOLOR error")
      Assert.AreEqual(50.0, .TimeSpent, "TIMESPENT error")
      Assert.AreEqual("d:\test", .FileRefPath, "FILEREFPATH error")
      Assert.AreEqual(8, .Id, "ID error")
      Assert.AreEqual(10.0, .Cost, "COST error")
      Assert.AreEqual(0, .CalcTimeEstimate, "CALCTIMEESTIMATE error")
      Assert.AreEqual(10.0, .CalcCost, "CALCCOST error")
      Assert.AreEqual(100, .PercentDone, "PERCENTDONE error")
      Assert.AreEqual("3", .Depends, "DEPENDS error")
      Assert.AreEqual(39515.0, .StartDate, "STARTDATE error")
      Assert.AreEqual(39515, .DueDate, "DUEDATE error")
      Assert.AreEqual("8-3-2008", .DueDateString, "DUEDATESTRING error")
      Assert.AreEqual(39514, .DoneDate, "DONEDATE error")
      Assert.AreEqual("7-3-2008", .DoneDateString, "DONEDATESTRING error")
      Assert.AreEqual(1, .Position, "Position error")
    End With
  End Sub

  <Test()> _
  Sub TestGetTask()

    'get a root task
    Assert.IsNotNull(mTodoList.GetTask(8), "roottask 8 not found")
    Assert.AreEqual("Task with all properties", mTodoList.GetTask(8).Title, "task '8' has wrong title: wrong task returned?")

    'get a sub task
    Assert.IsNotNull(mTodoList.GetTask(3), "subtask 3 not found")
    Assert.AreEqual("SubTask 1", mTodoList.GetTask(3).Title, "Wrong title for task 3: wrong task returned?")

    'get a subsub task
    Assert.IsNotNull(mTodoList.GetTask(4), "subsubtask 4 not found")
    Assert.AreEqual("SubSubTask 1", mTodoList.GetTask(4).Title, "subsubtask 4 has wrong title: wrong task?")

    'not existant task
    Assert.IsNull(mTodoList.GetTask(1234), "non existant task should not be found")
  End Sub

  <Test()> _
  Sub TestGetParent()

    Dim lSubTask As Model.Task = mTodoList.GetTask(12)
    Assert.IsNotNull(lSubTask, "task 12 not found, needed for test")

    Dim lParent As Model.Task = mTodoList.GetParentTask(lSubTask)
    Assert.IsNotNull(lParent, "Parent not found")
    Assert.AreEqual(8, lParent.Id, "Parenttask is not the right one")

    Assert.IsNull(mTodoList.GetParentTask(mTodoList.GetTask(8)), "Root element should not have a parent task")
  End Sub

End Class
